package com.firebird.app.kotlin.stock.ui.addbank


import android.app.Dialog
import android.os.Bundle
import android.text.TextUtils
import android.view.KeyEvent
import android.view.View
import android.view.inputmethod.EditorInfo
import android.widget.EditText
import android.widget.LinearLayout
import android.widget.RelativeLayout
import android.widget.TextView

import com.firebird.app.kotlin.stock.Config
import com.firebird.app.kotlin.stock.R
import com.firebird.app.kotlin.stock.base.BaseMvpActivity
import com.firebird.app.kotlin.stock.data.BankData
import com.firebird.app.kotlin.stock.data.EventMessage
import com.firebird.app.kotlin.stock.data.SimpleBean
import com.firebird.app.kotlin.stock.ui.addbank.contract.AddBankContract
import com.firebird.app.kotlin.stock.ui.addbank.presenter.AddBankPresenter
import com.firebird.app.kotlin.stock.ui.deposit.DepositActivity
import com.firebird.app.kotlin.stock.utils.DialogUtils
import com.firebird.app.kotlin.stock.utils.MaptoJsonUtils
import com.firebird.app.kotlin.stock.utils.SharedPreferencesUtil
import com.firebird.app.kotlin.stock.utils.ShowDialogUtils
import com.firebird.app.kotlin.stock.utils.ToastUtil

import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

import butterknife.BindView
import butterknife.ButterKnife
import butterknife.OnClick
import butterknife.OnEditorAction
import butterknife.OnTextChanged

class AddBankActivity: BaseMvpActivity<AddBankPresenter?>() , AddBankContract.View {

    @BindView(R.id.back)
    private var back:LinearLayout?=null
    @BindView(R.id.tv_title)
    private var tvTitle:TextView?=null
    @BindView(R.id.tv_name)

    private var tvName:TextView?=null
    @BindView(R.id.tv_idcard)
    private var tvIdcard:TextView?=null
    @BindView(R.id.et_bankid)
    private var etBankid:EditText?=null
    @BindView(R.id.tv_bank)
    private var tvBank:TextView?=null
    @BindView(R.id.rl_chooseaddress)
    private var  rlChooseaddress:RelativeLayout?=null
    @BindView(R.id.tv_next)
    private var tvNext:TextView?=null
    private var strIdcard:String?=null
    private var strMessage:String?= null
    private var isdeal:String?=null
    private var  dialog:Dialog?=null

    override fun getLayoutId():Int {
        return R.layout.activity_add_bank
    }

    override fun  initView() {
        tvTitle?.text="添加银行卡"
        mPresenter =  AddBankPresenter()
        mPresenter?.attachView(this)

        val username :String= SharedPreferencesUtil.getInstance(this)!!.getSP("username")
        var idcard:String = SharedPreferencesUtil.getInstance(this)!!.getSP("idcard")
        isdeal = getIntent().getExtras().getString("isdeal")
        tvName?.text=username
        //tvIdcard?.text=HideUtil.hideCardNo(idcard)
    }

    override fun onStart() {
        super.onStart()
        EventBus.getDefault().register(this)
    }

    @OnTextChanged(R.id.et_bankid)
    override fun onTextChanged( text:CharSequence) {
        //内容改变监听
        strIdcard = text.toString()
        val length:Int = strIdcard!!.length
        if (length > 15) {
            keyList.add("cardNo")
            valueList.add(strIdcard!!)
            mPresenter?.getData(MaptoJsonUtils.Simple(keyList, valueList))
            keyList.clear()
            valueList.clear()
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    fun onReceiveMsg( message:EventMessage) {
        strMessage = message.message
    }

    @OnClick({R.id.back,R.id.tv_next})
    fun onViewClicked( view:View) {
        when (view.getId()) {
             R.id.back->
                finish()
            R.id.tv_next->
                if (TextUtils.isEmpty(tvBank!!.getText().toString().trim())) {
                    ToastUtil.showToast(this, "请输入正确银行卡号");
                } else {
                    keyList.add("cardNo")
                    keyList.add("debtorId")
                    valueList.add(strIdcard!!)
                    valueList.add(strUserid!!)
                    mPresenter!!.getBindData(MaptoJsonUtils.Simple(keyList, valueList));
                }

        }
    }

    override fun showLoading() {
        dialog = DialogUtils.showDialog_progressbar(this)
    }

    override fun hideLoading() {
        DialogUtils.closeDialog(dialog)
    }

    override fun onError(throwable: Throwable?) {
        DialogUtils.closeDialog(dialog)
    }

    override fun onSuccess(bean: BankData?, token: String?) {
        if (bean.respCode.equals(Config.SUCCESS)) {
            tvBank?.setText(bean?.bankName)
            SharedPreferencesUtil.getInstance(this)!!.putSP("token", token);
        } else if (bean.respCode.equals(Config.TOKENOUT)) {
            ShowDialogUtils.restLoginDialog(this);
        } else {
            tvBank.setText("");
            ToastUtil.showToast(this, bean?.respDesc)
        }
    }

    override fun onSuccess(bean:SimpleBean?) {
        if (bean.respCode.equals(Config.SUCCESS)) {
            if (isdeal.equals("no")) {
                //第一次添加，需要设置交易密码
                //toClass1(this, InputPwdActivity.class);
            } else {
                //不是第一次添加，不需要设置
                if (strMessage.equals("MyBank") || strMessage.equals("Person")) {
                    //跳转到银行卡界面
                    //toClass_EmptyTop(this, PersonalActivity.class);
                }
                if (strMessage.equals("Deposit")) {
                    //跳转到提现界面

                   // toClass_EmptyTop(this, DepositActivity.class)
                }
            }

        } else if (bean?.respCode.equals(Config.TOKENOUT)) {
            ShowDialogUtils.restLoginDialog(this);
        } else {
            ToastUtil.showToast(this, bean?.respDesc);
        }
    }

    override fun onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}